package logic

import (
	"context"
	"errors"
	"shop-hub/models/mysql"
	"shop-hub/shoprpc/internal/svc"
	"shop-hub/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *shoprpc.ShopAddRequest) (*shoprpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User

	mysql.DB.Debug().Where("account = ?", in.Account).Find(&findUser)

	if findUser.ID != 0 {
		return nil, errors.New("当前账号已存在,请更换账号")
	}

	user := mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Create(&user)

	return &shoprpc.ShopAddResponse{
		UserId: int64(user.ID),
	}, nil
}
